import Vue from 'vue'

import core from 'gapi/core'

import commonPage from 'clientApi/commonPage.js'

import location from 'gapi/location.js'

import cache from 'gapi/cache.js'

import App from './App'

import searchPage from 'clientApi/searchPage.js'

import widget from 'gapi/widget'

import * as util from 'js/util.js'

import Empty from 'components/Empty/Empty'

new core('doumi').start(() => {

	new Vue({
		el: 'body',
		data:{
			searchInput:'',
			map:{},
			searchResultList:[],
            mPoint: {},
            tune_distance: 1000,
            isShowEmpty: false,//第一次进来不显示空态页  搜索后显示
            locationList: []//带来的 搜索范围 地点数组
		},
		components: {
            App,
            Empty
		},
		methods:{
            searchAddress(){
                if(this.tune_distance){
                    //有范围 地点微调
                    // alert(123)
                    this.searchAddressInfo()
                }else{
                    //无范围搜索
                    var t = this
                    t.searchResultList = []
                    if(t.searchInput.length){
                        var options = {
                            pageCapacity:20,//设置搜索结果条数
                            onSearchComplete: function(results){
                                console.log(results)
                                t.isShowEmpty = true
                                // 判断状态是否正确
                                for (var j = 0; j < results.getCurrentNumPois(); j ++){
                                    let s = {}
                                    s.id = j
                                    s.name = results.getPoi(j).title
                                    s.address = results.getPoi(j).address
                                    s.coordinate = results.getPoi(j).point
                                    t.searchResultList.push(s)
                                }
                            }
                        }
                        var local = new BMap.LocalSearch(t.map, options)
                        // if(this.tune_distance){
                            // alert('签到')
                            // local.searchNearby(t.searchInput, new BMap.Point(this.mPoint.lon, this.mPoint.lat),this.tune_distance);
                        // }else{
                            // alert('考勤')
                            local.search(t.searchInput)
                        // }
                    }
                }
                
			},
            selectPosition(position){
                commonPage.goBack(1,{'key':'kqPositionSearchResult','value':position})
            },
             searchAddressInfo(){
                var that = this
                let keyWord = this.searchInput;
                that.isShowEmpty = true
                that.searchResultList = []
                // if (keyWord.length > 0 ){
                    if(typeof that.locationList !== 'object'){
                        return false;
                    }
                    else{
                        var found = [];
                        for(var i = 0 ;i < that.locationList.length ;i++){
                            var str = that.locationList[i].name
                            // if (that.locationList[i].name.indexOf(keyWord) != -1 ||  that.locationList[i].address.indexOf(keyWord) != -1) {
                            if (that.locationList[i].name.indexOf(keyWord) != -1 ) {
                                
                                let s = {}
                                s.id = i
                                s.name = that.locationList[i].name
                                s.address = that.locationList[i].address
                                s.coordinate = that.locationList[i].coordinate
                                that.searchResultList.push(s)
                                console.log(this.searchResultList)
                            }
                        }
                    }     
                // }
            },
            
            selectPositionInfo(position){
                // this.searchResultList = [];
                // var $searchBar = $('#searchBar')
                // var $searchText = $('#searchText')
                // var $searchInput = $('#searchInput')

                // $searchBar.removeClass('weui-search-bar_focusing');
                // $searchText.show();
                // $searchInput.val('');
                // $searchInput.blur();
                // if(position)
                // {
                //     this.addressList = []
                //     this.addressList.push(position)
                //     this.switchView(position) //设置默认选中地点
                //     this.selectId = this.addressList[0].id
                // }
            }
		},
		ready(){
            this.tune_distance = Number(util.getUrddataString('tune_distance'))
            this.locationList = JSON.parse(util.getUrddataString('locationList'))

            // alert(JSON.stringify(this.locationList))
            if(!this.tune_distance){
                var t = this
                location.requestPOI(function(data){
                    // 搜索当前位置附近的商场等
                    let lon = 116.404
                    let lat = 39.915
                    if(data&&data.lon&&data.lat){
                        lon = data.lon
                        lat = data.lat
                        t.mPoint.lon = data.lon
                        t.mPoint.lat = data.lat
                    }else{
                        widget.toast(data.error_msg)
                    }
                    t.map = new BMap.Map('l-map')
                    t.map.centerAndZoom(new BMap.Point(lon,lat), 12)//FIXME 定位
                })
            }
            
			window.getSearchResult=(data)=>{
                this.searchInput = data
                this.searchAddress()
            }
		}
	})

})